import React, { Component, PropTypes } from "react";
import { messages } from "share/common";
import { Button, message, Modal } from 'antd'
import borrowService from "../../borrow-and-return.service";

class BtnRecall extends Component {

  state = {
    loading: false
  };

  handleConfirm = () => {
    Modal.confirm({
      title: messages('finance-1.key107')/*撤回单据*/,
      content: messages('finance-1.key108')/*确定撤回该单据？*/,
      okText: messages('common.ok')/*确定*/,
      onOk: this.handleRecall
    })
  };

  handleRecall = () => {
    const { info, close } = this.context;
    let params = {
      entities:[{
        entityOID: info.loanBillOID,
        entityType: 3001
      }]
    };
    this.setState({ loading: true });
    borrowService.recallRequest(params).then(res => {
      this.setState({ loading: false });
      if (res.data.failNum) {
        let reason = '';
        Object.keys(res.data.failReason).map(key => {
          reason = res.data.failReason[key]
        });
        message.error(`${messages('common.operate.filed')/*操作失败*/}，${reason}`)
      } else {
        message.success(messages('common.operate.success')/*操作成功*/);
        close(true);
      }
    }).catch(e => {
      this.setState({ loading: false });
      message.error(`${messages('common.operate.filed')/*操作失败*/}，${e.response.data.message}`)
    })
  };

  render() {
    const { loading } = this.state;
    return (
      <Button
        type="primary"
        loading={loading}
        onClick={this.handleConfirm}
      >
        {messages('finance-1.key107')/*撤回单据*/}
      </Button>
    )
  }

  static contextTypes = {
    info: PropTypes.object,
    close: PropTypes.func
  }

}

export default BtnRecall
